Calculating principal and interest payments for a loan using Python and numpy

Overview:

  • The Python library numpy has the functions ppmt() and ipmt() to calculate the monthly principal payment and interest payment.
  • For a given loan, the sum of the return value of the above two functions is equal to the value returned by the function pmt().

Example:

import numpy as np

 

interestRate        = 0.07

numberOfMonths      = 25*12;

principalBorrowed   = 3500000

 

principal2Pay   = np.ppmt(interestRate/12, 1, numberOfMonths, principalBorrowed);

interest2Pay    = np.ipmt(interestRate/12, 1, numberOfMonths, principalBorrowed);

 

print("Loan amount:%7.2f"%principalBorrowed);

print("Loan duration in months:%d"%numberOfMonths);

print("Annual Interest Rate in percent:%2.2f"%(interestRate*100));

 

print("Principal to be paid:%5.2f"%abs(principal2Pay));

print("Interest to be paid:%5.2f"%abs(interest2Pay));

print("Principal+Interest, to be paid:%5.2f"%abs(principal2Pay+interest2Pay));

 

Output:

Loan amount:3500000.00

Loan duration in months:300

Annual Interest Rate in percent:7.00

Principal to be paid:4320.61

Interest to be paid:20416.67

Principal+Interest, to be paid:24737.27


Copyright 2024 © pythontic.com